package com.example.yls.demoa;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.xmlpull.v1.XmlPullParser;

import java.util.List;

/**
 * Created by yls on 2018/6/28.
 */

public class StudentAdapter extends ArrayAdapter {

    private final int resourceId;

    public StudentAdapter(@NonNull Context context, int textViewResourceId, List<Student> objects) {
        super(context,textViewResourceId,objects);

        resourceId = textViewResourceId;

    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Student student = (Student) getItem(position);
        View view;
        ViewHolder viewHolder;
        if(convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resourceId,parent,
                    false);
            viewHolder = new ViewHolder();
            viewHolder.studentName = view.findViewById(R.id.tv_name);
            viewHolder.studentAge = view.findViewById(R.id.tv_age);
            viewHolder.studentSex = view.findViewById(R.id.tv_sex);
            view.setTag(viewHolder);
        }else{
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.studentName.setText(student.getName());

        return view;

    }

    class ViewHolder{
        TextView studentAge;
        TextView studentSex;
        TextView studentName;
    }

}
